package cn.com.nou.common.utils;


import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.springframework.util.StringUtils;
/**
 * 
 * @ClassName: TrimBeanUtil 
 * @Description: TODO 
 * @author yuanyang
 * @date 2018年12月19日
 */
public class TrimBeanUtil {
    
    public static void trimBean(Object model) throws Exception {
        Class clazz = model.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getGenericType().toString().equals("class java.lang.String")) {
                Method getMethod = clazz.getMethod("get" + getMethodName(field.getName()));
                String value = (String) getMethod.invoke(model);
                if (!StringUtils.isEmpty(value)) {
                    value = value.replaceAll("\\s*", "");
                    field.setAccessible(true);
                    field.set(model, value);
                }
            }
        }
    }


    private static String getMethodName(String fildeName) throws Exception {
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }
    
   
    
}